package com.demo.controller;

import com.alibaba.fastjson2.JSON;
import com.demo.common.ResultData;
import com.demo.request.NoticeReq;
import com.demo.request.NoticeTemplateReq;
import com.demo.sender.notice.NoticeSender;
import com.demo.util.TemplateReplaceUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 消息通知集成
 *
 * @author wangfengchen
 */
@Slf4j
@RestController
@RequestMapping("/notice")
public class NoticeController {

  @Autowired
  private NoticeSender noticeSender;

  /**
   * 发送消息
   *
   * @param req
   * @return
   */
  @PostMapping("/send")
  public ResultData<Object> send(@RequestBody NoticeReq req) {
    ResultData<Object> resultData = noticeSender.send(req);
    return ResultData.success(resultData);
  }

  /**
   * 发送模版消息
   *
   * @param req
   * @return
   */
  @PostMapping("/sendTemplate")
  public ResultData<Object> sendTemplate(@RequestBody NoticeTemplateReq req) {
    String templateCode = "LARK_TEXT";
    String templateStr = "您正在${operate}，您的验证码为${authCode}，请妥善保管！";

    String param = "{\"operate\":\"注册\",\"authCode\":\"123456\"}";


    String s = "SINOHOPE-[orgName]-[currentUser]TAP操作\n" +
        "\n" +
        "操作名称：operateName\n" +
        "所属金库：vaultName\n" +
        "操作人：currentUser\n" +
        "操作人IP地址：ip\n" +
        "操作时间：operateTime";

    Map<String, Object> map = JSON.parseObject(param);
    String content = TemplateReplaceUtil.replaceWithMap(templateStr, map);
    req.setContent(content);
    ResultData<Object> resultData = noticeSender.send(req);
    return ResultData.success(resultData);
  }


}
